package com.partvoyage.client.mvp2.view.carte;

import com.google.gwt.maps.client.MapPane;
import com.google.gwt.maps.client.MapPaneType;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.geom.Point;
import com.google.gwt.maps.client.overlay.Overlay;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.SimplePanel;
import com.smartgwt.client.widgets.layout.HLayout;

public class MapMarkerTextOverlay extends Overlay {

	private final LatLng latLng;

	private final SimplePanel textPanel;
	// private final HLayout textPanel;

	private MapWidget parentMap;

	private MapPane pane;

	private String text;

	private Point offset;

	/**
	 * Main constructor
	 * 
	 * @param latLng
	 */
	public MapMarkerTextOverlay(LatLng latLng, String text, Point offset) {
		/* Save our inputs to the object */
		this.latLng = latLng;
		this.text = text;
		this.offset = offset;

		/* Create a widget for the text */
		HTML textWidget = new HTML(text);

		/* Create the panel to hold the text */
//		textPanel = new HLayout();
		textPanel = new SimplePanel();
//		textPanel.setSize("30px", "30px");
		textPanel.setStyleName("markerStyle");
		
//		textPanel.addChild(textWidget);
		textPanel.add(textWidget);

		/* Panel gets added to the map and placed in the initialize method */
	}

	@Override
	protected final void initialize(MapWidget map) {
		/* Save a handle to the parent map widget */
		parentMap = map; // If we need to do redraws we'll need this

		/* Add our textPanel to the main map pane */
		pane = map.getPane(MapPaneType.MARKER_PANE);
		pane.add(textPanel);

		/* Place the textPanel on the pane in the correct spot */
		Point locationPoint = parentMap.convertLatLngToDivPixel(getLatLng());
		Point offsetPoint = Point
				.newInstance(locationPoint.getX() - getOffset().getX(),
						locationPoint.getY() - getOffset().getY());
		pane.setWidgetPosition(textPanel, offsetPoint.getX(),
				offsetPoint.getY());
	}

	@Override
	protected final Overlay copy() {
		return new MapMarkerTextOverlay(getLatLng(), getText(), getOffset());
	}

	@Override
	protected final void redraw(boolean force) {
		// Shouldn't need to do anything here since we're on the Marker pane.
	}

	@Override
	protected final void remove() {
		textPanel.removeFromParent();
	}

	public LatLng getLatLng() {
		return latLng;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public Point getOffset() {
		return offset;
	}

	public void setOffset(Point offset) {
		this.offset = offset;
	}

}